// app.js
import { getloginCode, codeToToken, checkSession, checkToken } from "./service/api_login"
App({
  // 全局数据
  globalData: {
    screenWidth: 0,      // 屏幕宽度
    screenHeight: 0,     // 屏幕高度
    statusBarHeight: 0,  // 状态栏高度
    deviceRadio: 0,      // 屏幕宽高比
  },
  onLaunch: async function () {
    const info = wx.getSystemInfoSync()
    this.globalData.screenWidth = info.screenWidth
    this.globalData.screenHeight = info.screenHeight
    this.globalData.statusBarHeight = info.statusBarHeight
    this.globalData.deviceRadio = info.screenHeight / info.screenWidth
    wx.setStorageSync('cookies', "MUSIC_U=b604f28c0e677e478710c7673c221aa84e72c3a986bf544ca957151692914842993166e004087dd3b5f2405bdd1bb0cbafac61b420f81867d42106b30fa748fa7759291272c260f0a89fe7c55eac81f3;")
    // 用户登录获取token
    // this.handleLogin()
    // 获取用户信息()
    // this.getUserInfo()
  },
  getUserInfo(){
  },
  async handleLogin() {
    const token = wx.getStorageSync('token_key')
    const checkResult = await checkToken(token)   // 是否token过期
    const checkSessionExpire = await checkSession()  // 是否session 过期
    // 判断是否需要重新登录
    if (!token || checkResult.errorCode || !checkSessionExpire) {
      this.loginAction()
    }
  },
  async loginAction() {
    const code = await getloginCode()
    const { data } = await codeToToken(code)
    wx.setStorageSync('token_key', data.token)
  }
})
